//postcss插件
import { Plugin } from "postcss"

const Options = {
  viewportWidth: 375,//默认宽度
}
interface Options {
  viewportWidth?:number
}

export const PostCssToViewport = (options:Options = Options):Plugin =>{
  const opt = Object.assign({},Options,options);
  return {
    postcssPlugin: 'postcss-px-to-viewport',
    //钩子函数
    Declaration(node){
      
      if(node.value.indexOf('xm') != -1){
        console.log(node.prop,node.value);
        const num = parseFloat(node.value);
        node.value = `${((num / opt.viewportWidth)*100).toFixed(2)}vw`
      }
      
    }
  }
}